<?php
class VideosController extends AppController {

	var $name = 'Videos';
	var $helpers = array('Html', 'Form', 'Time','Flash','Javascript');

	function index() {
		$this->Video->recursive = 0;
		if (isset($this->params['requested']))
		{
			return $this->Video->find('all', array('conditions'=>array('activo'=>1),'order'=>'orden ASC'));
		}
	}


	function admin_index() {
	if(isset($this->params['admin']) && $this->params['admin'])
	{
			// check that a user is logged in
			    $this->check_user();
	}
	$this->layout='admin';
		$this->Video->recursive = 0;
		$this->set('videos', $this->paginate());
	}

	function admin_view($id = null) {
	if(isset($this->params['admin']) && $this->params['admin'])
	{
			// check that a user is logged in
			    $this->check_user();
	}
	$this->layout='admin';
		if (!$id) {
			$this->Session->setFlash(__('Invalid Video.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('video', $this->Video->read(null, $id));
	}

	function admin_add() {
	if(isset($this->params['admin']) && $this->params['admin'])
	{
			// check that a user is logged in
			    $this->check_user();
	}
	$this->layout='admin';
		if (!empty($this->data)) {
			$this->Video->create();
			
			if(!empty( $this->data['Video']['file']['name'] ))
			{
				$foto = $this->data['Video']['file']  = $this->subir_archivo('img/videos/', $this->data['Video']['file']);
				$this->resize_img(WWW_ROOT . 'img/videos/'. $foto, 200, WWW_ROOT . 'img/videos/' . $foto) ;
				
							
			}
			else
			{
			    $this->data['Video']['file'] ='no-photo.jpg';
			}
			
			if ($this->Video->save($this->data)) {
				$this->Session->setFlash(__('The Video has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Video could not be saved. Please, try again.', true));
			}
		}
	}

	function admin_edit($id = null) {
	if(isset($this->params['admin']) && $this->params['admin'])
	{
			// check that a user is logged in
			    $this->check_user();
	}
	$this->layout='admin';
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Video', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
		
		//Inicia actualizacicion de foto			
		$this->data_update = $this->Video->read(null, $id);
		if( !empty($this->data['Video']['file']['name']))
		{
		    
		    
		    $foto = $this->data_update['Video']['file']; 
		    if(file_exists( WWW_ROOT .'img/videos/'.$foto) && $foto != "" )
		    {
		        unlink(WWW_ROOT .'img/videos/'.$foto);
		    }
		
		    $foto = $this->data['Video']['file'] = $this->subir_archivo('img/videos/', $this->data['Video']['file']);
		    $this->resize_img(WWW_ROOT . 'img/videos/'. $foto, 200, WWW_ROOT . 'img/videos/' . $foto) ;
		    
		
		}
		else
		{
		    $this->data['Video']['file'] = $this->data_update['Video']['file']; 
		}
		//FIN actualizacicion de foto
		
			if ($this->Video->save($this->data)) {
				$this->Session->setFlash(__('The Video has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Video could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Video->read(null, $id);
		}
	}

	function admin_delete($id = null) {
	if(isset($this->params['admin']) && $this->params['admin'])
	{
			// check that a user is logged in
			    $this->check_user();
	}
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Video', true));
			$this->redirect(array('action'=>'index'));
		}
		
		$this->data_update = $this->Video->read(null, $id);    
		$foto = $this->data_update['Video']['file'];
		
		if( file_exists( WWW_ROOT .'img/videos/'.$foto) && $foto != "" )
		{
			unlink(WWW_ROOT .'img/videos/'.$foto);
		}
					    

		if ($this->Video->del($id)) {
			$this->Session->setFlash(__('Video deleted', true));
			$this->redirect(array('action'=>'index'));
		}
	}

}
?>
